package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Leave;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.query.LeaveQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ILeaveService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("business/leave")
public class LeaveController {

    @Autowired
    private ILeaveService leaveService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    
    //模板前缀
    private static final String prefix = "business/leave/";
    //页面------------------------------------------------------------
    //列表
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequestMapping("/auditPage")
    public String auditPage(@ModelAttribute("id") Long id){
        return prefix + "auditPage";
    }

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Leave> query(LeaveQuery qo){
        qo.setLoginUsername(ShiroUtils.getUser().getUserName());
        return leaveService.query(qo);
    }

    //页面------------------------------------------------------------
    //列表
    @RequestMapping("/donePage")
    public String donePage (){

        return prefix + "done";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<Leave> doneQuery(LeaveQuery qo){
        return leaveService.query(qo);
    }

    //页面------------------------------------------------------------
    //列表
    @RequestMapping("/todoPage")
    public String todoPage (){

        return prefix + "todo";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<Leave> todoQuery(LeaveQuery qo){
        qo.setTodoUserId(ShiroUtils.getUser().getId());
        return leaveService.query(qo);
    }


    @RequestMapping("/startAuditPage")
    public String startAudit(Model model, Long id){
        User user = ShiroUtils.getUser();
        model.addAttribute("user",user);

        //查询店长和财务专员的数据放到作用域中去
        List<User> shopOwners = userService.queryByRoleKey("shopOwner");
        model.addAttribute("directors",shopOwners);
        List<User> financials = userService.queryByRoleKey("financial");
        model.addAttribute("finances",financials);

        //查询发起审核的服务类型也放到作用域中去
        //因为这个服务流程可能有多个,我们只需要取最新的一个,所以直接按部署时间降序查询取第一个就可以了
        List<BpmnInfo> bpmnInfos = bpmnInfoService.queryByBpmnType("casual-leave");
        BpmnInfo bpmnInfo = bpmnInfos.get(0);
        model.addAttribute("bpmnInfo",bpmnInfo);


        return prefix + "audit";
    }

    @RequestMapping("startAudit")
    @ResponseBody
    public AjaxResult startAudit(Long id, Long bpmnInfoId,
                                 Long director, Long finance,Leave leave){
        leaveService.startAudit(id,bpmnInfoId,director,finance,leave);
        return AjaxResult.success();
    }


    @RequestMapping("audit")
    @ResponseBody
    public AjaxResult audit(Long id,Long auditStatus,String info){
        leaveService.audit(id,auditStatus,info);
        return AjaxResult.success();
    }

    @RequestMapping("/processImg")
    @ResponseBody
    public void processImg(HttpServletResponse response, Long id) throws IOException {
        InputStream inputStream = leaveService.processImg(id);
        IOUtils.copy(inputStream,response.getOutputStream());
    }








}
